package com.fingard.dsp.bank.directbank.bbg02;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * @author Parker on 2020-04-21.
 * @version 1.0
 */
public class BBG02Balance extends BBG02Base implements IBalance {


    /**
     * 查询余额
     * @param p_req7506
     * @return
     * @throws Exception
     */
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts7506BalQry = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts7506BalQry.writeStart();
        try{
            while (p_req7506.readRecord()) {
                RetAtsBalDetail detail = new RetAtsBalDetail();
                //非公有参数
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                //拼接请求报文
                BBG02Header header = new BBG02Header();
                header.tr_code="200108";
                header.req_no = p_req7506.reqHeader.reqSeqID;
                writeCommonHeader(xmlTextWriter,header);
                //获取币种和账号
                XElement body =new XElement("body","");
                body.addChild(new XElement("acno",p_req7506.readedDetail.actNum));//账号
                body.addChild(new XElement("cur_code",convertCurToBank(p_req7506.readedDetail.actCur)));//币种
                xmlTextWriter.writeXElement(body);
                xmlTextWriter.writeEndDocument();

                //发送请求报文
                String[] bankResult = sendToBank(xmlTextWriter.toXmlString(),p_req7506.reqHeader.transType,p_req7506.reqHeader.commandCode);
                //WriteBankLogStep2(head+xmlTextWriter.toXmlString());
                //String[] bankResult=new String[]{"","000000055200<ap>    <head>        <tr_code></tr_code>        <org_code>0000</org_code>        <cms_corp_no>10000044</cms_corp_no>        <user_no>123123123</user_no>        <serial_no></serial_no>        <req_no></req_no>        <tr_acdt>20190118</tr_acdt>        <tr_time>085133</tr_time>        <channel>5</channel>        <sign>0</sign>        <file_flag>0</file_flag>        <reserved></reserved>        <succ_flag>1</succ_flag>        <ret_code>9999</ret_code>        <ret_info>????????????????</ret_info>        <ext_info>??????????[&lt;ap&gt;    &lt;h]</ext_info>    </head></ap>"};

                //处理响应报文
                if (bankResult[0].length() == 0) {
                    StringBuilder tmpSb = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1].substring(bankResult[1].indexOf("<ap>")), tmpSb);
                    if (tmpBkReader != null) {
                        String succFlag= tmpBkReader.getFirstText("/ap/head/succ_flag");
                        String respCode = tmpBkReader.getFirstText("/ap/head/ret_code");
                        String respInfo = tmpBkReader.getFirstText("/ap/head/ret_info");
                        if ("0".equals(succFlag) && "0000".equals(respCode)) {//交易成功
                            if (tmpBkReader.read("/ap/body")) {
                                detail.respCode = "0";
                                detail.respInfo = respInfo;
                                detail.actCur = p_req7506.readedDetail.actCur;
                                detail.actNum = p_req7506.readedDetail.actNum;
                                detail.availBal = tmpBkReader.readedXElement.getSingleTextNotNull("use_balance");
                                detail.currBal = tmpBkReader.readedXElement.getSingleTextNotNull("balance");
                                detail.frozenBal = tmpBkReader.readedXElement.getSingleTextNotNull("freeze_bal");
                            }
                        }else{
                            detail.respCode = "-1";
                            detail.respInfo = respInfo;
                        }
                    }else{
                        detail.respCode = "-1";
                        detail.respInfo = tmpSb.toString();
                    }
                }else{
                    detail.respCode = "-1";
                    detail.respInfo = bankResult[1];
                }
                retAts7506BalQry.writeRetDetail(detail, p_req7506.isBatch);
            }
        }catch (Exception e) {
            retAts7506BalQry.writeRespCode("-1");
            retAts7506BalQry.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts7506BalQry.writeEnd();
        return retAts7506BalQry;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
